package fragment;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dong.adapter.Adapter_list1;
import com.dong.utils.Uri2String;
import com.dong.yi18health.R;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

public class fragment_title2 extends Fragment {

	private View view;
	private int index = 1;
	List<News2> d = new ArrayList<>();

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.item2, null);
		initView();
		initListData();

		return view;
	}

	private void initView() {
		lv = (ListView) view.findViewById(R.id.item2_list2);
		lv.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView arg0, int arg1) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
				if(arg1+arg2==arg3){
					index=index+1;
					initListData();
				}
			}
		});
	}

	// 处理listView数据并填充
	Handler h = new Handler() {

		public void handleMessage(Message msg) {
			String result = (String) msg.obj;
			JSONObject o = JSON.parseObject(result);
			String s = o.getString("yi18");

			List<News2> da = JSON.parseArray(s, News2.class);
			d.addAll(da);
			Adapter_list1 ad = new Adapter_list1(d, getActivity());
			lv.setAdapter(ad);
		};
	};
	private ListView lv;

	private void initListData() {
		new Thread() {
			public void run() {
				String result2 = Uri2String.getResult("http://api.1ccf.com/lore/list?page=" + index + "&limit=20");
				Message m = new Message();
				m.obj = result2;
				h.sendMessage(m);
			};
		}.start();
	}
}
